package com.chinairi.mall.util;

import com.chinairi.mall.controller.viewobject.StAddress;
import com.chinairi.mall.dao.StorageAddressMapper;
import com.chinairi.mall.model.ProjectPlanReqStorage;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author yyp
 * @date 2020/11/18 14:36
 */
@Component
public class PurchaseUtils {

    /**
     * 根據需求數量獲取庫點編碼、平庫數量、庫存數量、平庫總數和庫存總數。
     *
     * @param reqNum 需求數量
     * @return
     */
    public static Map<String, Object> getFlatNum(float reqNum, String planReqId, String planMatCode) {
        StorageAddressMapper storageAddressMapper = MyApplicationContext.getBean(StorageAddressMapper.class);
        List<String> saDbidList = storageAddressMapper.getInfo();
        Map<String, Object> stMap = new HashMap<>(16);
        int stgNum = new Random().nextInt(4) + 1;
        //总平库数
        int num = 0;
        //总库存数
        int sNum = 0;
        List<ProjectPlanReqStorage> sList = new ArrayList<>();
        for (int i = 0; i < stgNum; i++) {
            if (reqNum - num >= 0) {
                //库存数量
                Integer sNum1 = Integer.valueOf(Math.abs(new Random().nextInt(1000))+(int)reqNum);
                //平库数量
                //Integer pNum = Integer.valueOf(new Random().nextInt((int)(Math.abs(reqNum - num - 1))) + 1);
                 //Integer pNum =sNum1<=(int)reqNum?sNum1:new Random().nextInt(sNum1 -(int)reqNum + 1)+(int)reqNum;
                //Integer pNum =sNum1<(int)reqNum?sNum1:new Random().nextInt((int)reqNum);
                System.out.println(sNum1 + "=================" + reqNum );
                Integer pNum =sNum1<(int)reqNum?sNum1:new Random().nextInt((int)reqNum)%((int)reqNum+1);
                System.out.println(reqNum + "=================" + pNum );
                if (pNum > 0) {
                    num += pNum;
                    if (num>reqNum){
                        int differ = num - (int)reqNum ;
                        differ = new Random().nextInt(num%(num - differ +1))+ differ ;
                        pNum = pNum - differ;
                         num = num - differ ;
                    }
                    ProjectPlanReqStorage projectPlanReqStorage = new ProjectPlanReqStorage();
                    projectPlanReqStorage.setDbid(UUID.randomUUID().toString().replace("-", ""));
                    projectPlanReqStorage.setPlanReqId(planReqId);
                    projectPlanReqStorage.setPlanMatCode(planMatCode);
                    projectPlanReqStorage.setFlatNum((float) pNum);
                    System.out.println(reqNum + "=================" + pNum );
                    projectPlanReqStorage.setSaDbid(String.valueOf(saDbidList.get(i)));
                    projectPlanReqStorage.setStorageNum((float) sNum1);
                    projectPlanReqStorage.setHavePickNum((float) 0);
                    sList.add(projectPlanReqStorage);
                   // num += pNum;
                    sNum += sNum1;
                }
            }
        }


        //库存总数数量
        stMap.put("allstor", sNum);
        //平库总数数量
        stMap.put("allflat", num);
        stMap.put("list", sList);
        return stMap;
    }

}
